/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.server.utils;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author aeky
 */
public final class RecordUtil {

    public static Map<String, Object> createBaseModelData(Object obj, String nonIncludeFields) {
        Map<String, Object> newMap = new HashMap<String, Object>();
        try {
            convertToMap("", obj, newMap, nonIncludeFields);
        } catch (Exception e) {

        }
        return newMap;
    }

    private static Map<String, Object> convertToMap(String fieldName, Object obj, Map<String, Object> hashMap,
            String nonIncudeFields) {
        if (obj == null) {
            return hashMap;
        }
        Method[] methods = obj.getClass().getMethods();// .getDeclaredMethods();
        for (Method method : methods) {
            try {
                String methodName = method.getName();
                String returnTypeName = method.getReturnType().getName();
                // org.hibernate.proxy
                if ((methodName.startsWith("get") || methodName.startsWith("is"))
                        && !returnTypeName.startsWith("org.hibernate") && method.getParameterTypes().length == 0) {
                    if ("getClass".equals(methodName)) {
                        continue;
                    }
                    String key = parseFiledName(fieldName, methodName);
                    if (nonIncudeFields.contains(key + ";")) {
                        continue;
                    }
                    Object subObj = method.invoke(obj, new Object[] {});
                    if (subObj == null) {
                        continue;
                    }

                    if (subObj instanceof Collection<?>) {
                        // Collection subCollection = (Collection) subObj;
                        // List<BaseModelData> subModelDataList =
                        // createBaseModelDataList(subCollection);
                        // hashMap.put(key, subModelDataList);
                    } else if (returnTypeName.startsWith("com.esi.bfp")) {
                        convertToMap(key, subObj, hashMap, nonIncudeFields);
                    } else if (subObj instanceof Date) {
                        hashMap.put(key, subObj);
                    } else if (subObj instanceof BigDecimal) {
                        // hashMap.put(key,
                        // ConvertUtils.convertBigDecimal2String((BigDecimal)
                        // subObj));
                    } else if (subObj instanceof Character) {
                        hashMap.put(key, subObj.toString());
                    } else {
                        hashMap.put(key, subObj);
                    }
                }
            } catch (Exception e) {
            }
        }
        return hashMap;
    }

    private static String parseFiledName(String parentFiled, String methodName) {
        String rst = parseFieldNameFromMethodName(methodName);
        if (parentFiled != null && !"".equals(parentFiled)) {
            rst = parentFiled + "." + rst;
        }
        return rst;
    }

    private static String parseFieldNameFromMethodName(String methodName) {
        if (methodName.startsWith("is")) {
            methodName = methodName.substring(2, methodName.length());
        } else {
            methodName = methodName.substring(3, methodName.length());
        }
        String fieldName = methodName.substring(0, 1).toLowerCase() + methodName.substring(1, methodName.length());
        return fieldName;

    }
}
